package com.bigShen.pattern.proxy.cglib;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

/**
 * @author: lei.shen
 * @date: 2018/10/2
 * @desc : CGLIB (Code Generation Library), 一个强大的开源项目, 可在运行期扩展Java类与实现Java接口
 */
public class LoveAgentCglib implements MethodInterceptor {


    public Object getInstance(Class<?> clazz) {
        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(clazz);
        enhancer.setCallback(this);
        return enhancer.create();
    }

    @Override
    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {

        System.out.println("Before : 婚介所帮忙寻找对象!");
        methodProxy.invokeSuper(o, objects);
        System.out.println("After : 婚介所收费!");
        return null;
    }

}
